<template>
  <div>
    <el-dialog v-model="dialogVisible" append-to-body title="用户充值" width="580">
      <div class="form-box">
        <el-form v-if="dialogVisible" label-width="132px" :model="formObj" ref="formRef" :rules="rules" size="large">
          <el-form-item label="Order ID">
            <span class="form-text">{{ formObj.order_nu }}</span>
          </el-form-item>
          <el-form-item label="Product type">
            <span class="form-text">{{ formObj.proxy_info }}</span>
          </el-form-item>
          <el-form-item label="Package type" prop="days">
            <el-select v-model="formObj.days" filterable clearable placeholder="Please select the package type">
              <!-- <el-option label="7天" value="7"></el-option> -->
              <el-option label="30天" value="30"></el-option>
              <el-option label="60天" value="60"></el-option>
              <el-option label="90天" value="90"></el-option>
              <el-option label="360天" value="360"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="Payment type" prop="pay_way">
            <el-select v-model="formObj.pay_way" filterable clearable placeholder="Please select payment type">
              <el-option label="AliPay" value="alipay" />
              <el-option label="Credit Card" value="card" />
              <el-option label="USDT" value="USDT" />
            </el-select>
          </el-form-item>
          <el-form-item label="支付金额" prop="amount">
            <div class="form-amount">
              <span>$</span>
              <strong>{{ amount }}</strong>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div style="text-align: right">
          <el-button size="large" @click="onClose()">取消</el-button>
          <el-button type="primary" size="large" :disabled="loading" :loading="loading" @click="onSubmit(formRef)">提交</el-button>
        </div>
      </template>
    </el-dialog>

    <Pay ref="payRef" payType="static" @comfirm="onComfirm" />
  </div>
</template>

<script lang="ts" setup>
  import { ref, watch } from 'vue';
  import { useRoute, useRouter } from 'vue-router';
  import { useUserStore } from '@/store/modules/user';
  import { buyStaticRenewApi } from '@/api/index';
  import type { FormInstance, FormRules } from 'element-plus';
  import { calculatePrice } from '@/utils/index';
  import Pay from '../../../pay/en.vue';

  const route = useRoute();
  const router = useRouter();
  const userStore = useUserStore();

  const getUserInfo = computed(() => {
    let userInfo = userStore.getUserInfo || {};
    return userInfo;
  });

  const amount = computed(() => {
    return formObj.line_type && formObj.days ? calculatePrice(formObj.line_type, formObj.days, 1) : '';
  });

  const emits = defineEmits<{
    (event: 'confirm'): void;
  }>();

  const loading = ref(false);
  const formRef = ref<FormInstance>();
  const formObj = reactive({
    days: '30',
    pay_way: 'alipay',
  });
  const dialogVisible = ref(false);
  const payRef = ref();

  const rules = reactive<FormRules>({
    days: [{ required: true, message: '请选择套餐', trigger: 'change' }],
    pay_way: [{ required: true, message: '请选择支付方式', trigger: 'blur' }],
  });

  const onOpen = (data) => {
    dialogVisible.value = true;
    Object.assign(formObj, data);
  };

  const onClose = () => {
    dialogVisible.value = false;
  };

  const onSubmit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    await formEl.validate((valid, fields) => {
      if (valid) {
        onSubmitFn();
      }
    });
  };

  const onSubmitFn = async () => {
    if (loading.value) return false;
    loading.value = true;
    buyStaticRenewApi(formObj)
      .then((res) => {
        loading.value = false;
        if (res.errcode == 0) {
          ElMessage.success(res.message || res.errmsg);
          dialogVisible.value = false;
          payRef.value.onComfirm(res.data);
        } else {
          ElMessage.error(res.message || res.errmsg);
        }
      })
      .catch((err) => {
        console.log('err', err);
        loading.value = false;
        ElMessage.error(err.message);
      });
  };

  const onComfirm = () => {
    dialogVisible.value = false;
    emits('confirm');
  };

  defineExpose({
    onOpen,
  });

  onMounted(() => {});
</script>

<style scoped lang="scss">
  .form-box {
    padding: 10px 20px 0 0;
  }

  .form-text {
    word-wrap: break-word;
    word-break: break-all;
    white-space: break-spaces;
    line-height: 24px;
  }

  .form-amount {
    display: inline-flex;
    flex-direction: row;
    justify-content: flex-start;
    align-content: center;
    align-items: center;
    height: 28px;
    font-size: 20px;
    color: #ff7928;
    font-weight: bold;
    line-height: 28px;

    strong {
      font-size: 28px;
      margin-top: -2px;
      margin-left: 4px;
    }
  }
</style>
